<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Request{
    protected $CI;
    /**
     * [__construct description]
     */
    public function __construct(){
        // Assign the CodeIgniter super-object
        $this->CI =& get_instance();
    }
    /**
     * [getPostData description]
     * @param  [type] $paramArray [description]
     * @return [type]             [description]
     */
    public function getPostData($paramArray=array())   {
        if(!array_key_exists("sf_session_key",$paramArray))
            array_push($paramArray, "sf_session_key");
        $data =  $this->CI->input->post($paramArray);
        return $data;
    }


    /**
     * [getPostData description]
     * @param  [type] $paramArray [description]
     * @return [type]             [description]
     */
    public function getGetData($paramArray=array())   {
        if(!array_key_exists("sf_session_key",$paramArray))
            array_push($paramArray, "sf_session_key");
        $data =  $this->CI->input->get($paramArray);
        return $data;
    }
    /**
     * [getPostJsonData description]
     * @return [type] [description]
     */
    public function getPostRawData(){
        $data =  $this->CI->input->raw_input_stream;
        if($data)
            return json_decode($data,true);
        else
            return array();

    }

    /**
     * GET 请求
     * @param string $url
     */
    public function http_get($url){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }
    /**
     * [http_post description]
     * @param  [type] $url  [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function http_post($url,$data){
        // echo $url;echo $data;exit;
        //初始化
        $curl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        // $post_data = array(
        //     "username" => "coder",
        //     "password" => "12345"
        //     );
        $headers = [
            "Content-type: application/json;charset='utf-8'",
            "Accept: application/json",
            "Cache-Control: no-cache",
            "Pragma: no-cache"
        ];
        // var_dump($headers);exit;
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $post_data = $data;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $sContent = curl_exec($curl);
        //显示获得的数据
        $aStatus = curl_getinfo($curl);
        //关闭URL请求
        curl_close($curl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }

    }

}